# -*- coding:utf-8 -*-
import requests
import time, Land


class RequestAssertion:
    # main_url_admin = 'http://admin-service-api.yhtest0.com/'
    token_admin = Land.token()

    def request(self, method, url, data=None, files=None, json=None, headers=None):
        if data is None:
            data = {}
        if files is None:
            files = {}
        if headers is None:
            headers = {"token": RequestAssertion().token_admin}

        respones = {}
        if method == "get":
            respones = requests.get(url=url, headers=headers)
        elif method == "post_x":
            respones = requests.post(url=url, data=data, headers=headers)
        elif method == "post":
            respones = requests.post(url=url, json=json, headers=headers)
        elif method == "put":
            respones = requests.put(url=url, json=data, headers=headers)
        elif method == "patch":
            respones = requests.patch(url=url, json=data, headers=headers)
        elif method == "del":
            respones = requests.delete(url=url, json=data, headers=headers)
        elif method == "files":
            respones = requests.post(url=url, data=data, files=files, headers=headers)
        return respones

    # 接口耗时
    def consuming(self, respones, interfaceName):
        time_consuming = respones.elapsed.total_seconds()
        if time_consuming >= 1:
            return interfaceName + ",Interface time: consuming over 1s,{}".format(time_consuming)
